﻿Imports System.Collections.Specialized
Imports System.IO
Imports System.Globalization
Imports Microsoft.TeamFoundation.Client
Imports Microsoft.TeamFoundation.WorkItemTracking.Client
Imports System.Collections.ObjectModel
Imports System

Namespace Controlers


    Public Class ControlerFactory(Of TView As {UIElement, IView})
        Implements IDisposable


        Private Shared m_MasterControler As ControlerFactory(Of TView)

        Private Sub New()

        End Sub

        Public Shared ReadOnly Property Instance() As ControlerFactory(Of TView)
            Get
                If m_MasterControler Is Nothing Then
                    m_MasterControler = New ControlerFactory(Of TView)
                End If
                Return m_MasterControler
            End Get
        End Property

        Private m_Controlers As New List(Of ControlerBase(Of TView))

        Public Sub AddControler(ByVal controler As ControlerBase(Of TView))
            controler.Factory = Me
            m_Controlers.Add(controler)
        End Sub

        Public Function GetControler(Of TControler As ControlerBase(Of TView))() As TControler
            Return m_Controlers.Find(Function(c) c.GetType Is GetType(TControler))
        End Function

#Region " IDisposable "


        Private disposedValue As Boolean = False        ' To detect redundant calls

        ' IDisposable
        Protected Overridable Sub Dispose(ByVal disposing As Boolean)
            If Not Me.disposedValue Then
                If disposing Then
                    ' TODO: free other state (managed objects).
                    For Each c In m_Controlers
                        c.Dispose()
                    Next
                    m_Controlers = Nothing
                End If

                ' TODO: free your own state (unmanaged objects).
                ' TODO: set large fields to null.
            End If
            Me.disposedValue = True
        End Sub

#Region " IDisposable Support "
        ' This code added by Visual Basic to correctly implement the disposable pattern.
        Public Sub Dispose() Implements IDisposable.Dispose
            ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub
#End Region

#End Region

    End Class

End Namespace